//
//  ShopQuanViewController.m
//  1243
//
//  Created by 杨森 on 16/4/8.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "ShopQuanViewController.h"
#import "ShopQuanTableViewCell.h"
#import "ShopQuanTwoTableViewCell.h"
#import "NetWorkTools.h"
#import "ConvertQuanViewController.h"
#import "ReadLoginData.h"

@interface ShopQuanViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *shopQuanTableView;

@property(nonatomic,assign)NSInteger gouWuQuanCount;


@end

@implementation ShopQuanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"购物券";
    [self addUI];
    
    [self loadGouWuQuanCount];
}

- (void)addUI
{
    CGFloat h = 40;
    UIView *lastView = [[UIView alloc] initWithFrame:CGRectMake(0,HEIGHT-h, WEITH, h)];
    
    lastView.backgroundColor = [UIColor whiteColor];
    
    UIButton *outDateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [lastView addSubview:outDateBtn];
    [outDateBtn setBackgroundImage:[UIImage imageNamed:@"查看兑换记录"] forState:UIControlStateNormal];
    
    CGFloat btnW = 100;
    outDateBtn.frame = CGRectMake(WEITH/2-btnW/2,10,btnW,15);
    
    self.shopQuanTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, WEITH, HEIGHT)];
    self.shopQuanTableView.delegate = self;
    self.shopQuanTableView.dataSource = self;
    
    [self.view addSubview:self.shopQuanTableView];
    [self.view addSubview:lastView];
    self.shopQuanTableView.rowHeight = 200;
    
//    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"兑换" style:UIBarButtonItemStylePlain target:self action:@selector(shopBtn)];
//    self.navigationItem.rightBarButtonItem = item;
    
}

- (void)shopBtn
{
    NSLog(@"使用说明");
    
    ConvertQuanViewController *conver = [[ConvertQuanViewController alloc]init];
    [self.navigationController pushViewController:conver animated:YES];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.gouWuQuanCount + 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if(indexPath.row < self.gouWuQuanCount ) {
        static NSString *identifier= @"convertCell";
        
        ShopQuanTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell==nil) {
            
            cell = [[[NSBundle mainBundle] loadNibNamed:@"ShopQuanTableViewCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        return cell;
        
    } else {
        
        static NSString *identifier= @"convertCell";
        
        ShopQuanTwoTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell==nil) {
            
            cell = [[[NSBundle mainBundle] loadNibNamed:@"ShopQuanTwoTableViewCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        return cell;
    }
    
}

- (void)loadGouWuQuanCount {
    
    NSDictionary *dict = [ReadLoginData readLoginData];
    
    NSString *userId = [dict objectForKey:@"Number"];
    
    NSDictionary *params = @{
                             @"UserNumber":userId
                             };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"BUYCARDCOUNT" completeBlock:^(id object) {
        NSLog(@"%@",object);
        
        self.gouWuQuanCount = [[object objectForKey:@"BackData"] integerValue];
//        self.gouWuQuanCount = 2;
        
        [self.shopQuanTableView reloadData];
    }];
}
@end
